package com.haisen.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class MyWC {



    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration(true);

        try {
            Job job = Job.getInstance(conf);
            job.setJarByClass(MyWC.class);

            job.setJobName("wordcount");

            job.setMapperClass(MyHDMapper.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);


            job.setCombinerClass(MyReduce.class);
            job.setReducerClass(MyReduce.class);

            // 设置输入和输出目录
            Path input = new Path("/user/root/test.txt");
            FileInputFormat.addInputPath(job, input);

            Path output = new Path("/user/wc/output");

            if (output.getFileSystem(conf).exists(output)){
                output.getFileSystem(conf).delete(output,true);
            }
            FileOutputFormat.setOutputPath(job, output);


            job.waitForCompletion(true);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }


}
